package com.itqf.controller;

import com.itqf.bean.User;
import com.itqf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * projectName: rbac2103
 *
 * @author: 赵伟风
 * time: 2021/7/8 11:36
 * description:用户登录和权限展示的controller
 */

@Controller
@RequestMapping("user")
@SessionAttributes(names = {"ret"})
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("index")
    public String indexHtml(){
        return "index";
    }


    @GetMapping("logout")
    public String logout(SessionStatus sessionStatus){

        //清空数据
        sessionStatus.setComplete();

        return "login";
    }

    @GetMapping("html")
    public String loginHtml(){
        System.out.println("跳转登录页面!");

        return "login";
    }

    @PostMapping("login")
    public ModelAndView login(User user){
        //没有实体类

        Map ret = userService.login(user);

        /**
         *
         * 失败
         *    code = 0
         * 成功
         *    code = 1
         *    permissions List<Permission>
         *    user  user
         */

        System.out.println("ret = " + ret);


        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("ret", ret);
        modelAndView.setViewName("index");

        return modelAndView;
    }


    /**
     * 未认证
     * @return
     */
    @GetMapping("noauthentication")
    public String noauthentication(){

        return "noauthentication";
    }


    /**
     * 未授权
     * @return
     */
    @GetMapping("noauthorization")
    public String noauthorization(){

        return "noauthorization";
    }
}
